<template>
	<view>
		<view class="vcxvrtytruytu pd mt40" @tap="jxcfter">
			<frImage src="../static/img/banner.png" class="w100 h100"></frImage>
		</view>
		<view class="pd pt20 pm20 mt20">
			<text class="xcxcrewcxr">精选话题</text>
		</view>
		<view class="pd pt20 mcxzcsdfrtry">
			<view class="pr" v-for="sd in list" @tap="xcxcrtytyur(sd)">
				<view class="xczxcrtreyxrt cz ov cz">
					<frImage :src="sd.skill_poster" class="w100 h100 cz"></frImage>
				</view>
				<view class="xcxctxdrty vertical-bottom pder pm20 ">

					<view class="" @tap.stop="cxzcsdsf(sd)">
						<image src="../static/img/shouchang.png" v-if="sd.favorite" class="xcvxcdytyyrtyr"></image>
						<image src="../static/img/shouchanga.png" v-else class="xcvxcdytyyrtyr"></image>
					</view> 
					<view class="w100 sdfdcxzcvxy pder pt20 pm20">
						<view class="dian cf fz28">
							{{sd.skill_name}}
						</view>
						<view class="fz22 cf mt10">
							{{sd.skill_desc}}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view> 
</template>
<script lang="ts" setup>
	import { onLoad } from "@dcloudio/uni-app"
	import { ref } from "vue"
	import { hf, dxrequest, callBack, LogBackin } from "@/util/index.js"
	import frImage from "@/components/fr-image/fr-image.vue"
	const list = ref([])
	 // uni.setStorageSync("token", "cb8f33427b520285cb51d150fe07eea074f73d27")
	const initsdr = async () => {
		const { data } = await dxrequest("api/child/skill/querySkillList?size=999")
		list.value = data.skill_list

	}
	const  getMobilePlatform=()=> {
    var userAgent = navigator.userAgent || navigator.vendor || window.opera;
 
    if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
        return 'iOS';
    } else if (userAgent.match(/Android/i)) {
        return 'Android';
    } else if (userAgent.match(/Windows Phone/i)) {
        return 'Windows Phone';
    } else {
        return 'unknown';
    }
}
	const isert = () => {
		return new Promise((resolve, reject) => {
				// 检查浏览器是否支持 getUserMedia API
				if (navigator.mediaDevices.getUserMedia) {
					// 设置我们想要获取的媒体类型
					const constraints = {
						audio: true
					};
		
					// 尝试获取麦克风权限
					navigator.mediaDevices.getUserMedia(constraints)
						.then(function(stream) {
							resolve(true)
						})
						.catch(function(error) {
							resolve(false)
						});
				} else {
					resolve(false)
				}
			})
	
	}
	initsdr()
	onLoad((data) => {
		if (window.code) {
			deljdr()
		}

	})
	const deljdr = async () => {
		const { obj } = await dxrequest("api/m/account/wxWebLogin", {
			code: window.code,
			shareScene: "mayaai",
			app_code: "maya-ai"
		})
		console.log(obj)
		localStorage.token = obj.token
		uni.setStorageSync("token", obj.token)
	}
	const xcxcrtytyur = (sd) => {
		let token = uni.getStorageSync("token")
		if(localStorage.token){
			token = localStorage.token
		}
		if (!token) {
			LogBackin()
		}else{
			if(getMobilePlatform()!='iOS'){
				isert()
			}
			
			hf('/pages/index/index?skill_code='+sd.skill_code)
		}


	}
	const cxzcsdsf =async(data)=>{
		const token = uni.getStorageSync("token")
		if (!token) {
			LogBackin()
		}
		await dxrequest("api/child/user/favorite",{
			"favorite_type": "skill", //收藏类型（skill=技能收藏）
			"favorite_code": data.skill_code, //收藏码（当skill=技能收藏时，favorite_code为该技能code）
			"favorite_status": data.favorite?2:1 //收藏状态（1=收藏；2=取消收藏）
		},"post")
		initsdr()
	}
	const jxcfter= ()=>{
	 uni.clearStorageSync()
	}
</script>

<style scoped>
	.vcxvrtytruytu {
		height: 340rpx;
	}

	.xcxcrewcxr {
		color: #3EFF77;
		font-size: 28rpx;
		border: 2rpx solid #3EFF77;
		padding: 10rpx 20rpx;
		border-radius: 40rpx;
	}
</style>